using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class DonViDAO
    {
        public static bool Insert(DonViDTO dv)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaDV",dv.MaDV));
                sqlparam.Add(new SqlParameter("@TenDonVi",dv.TenDonVi));
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spDonViInsert",sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool Delete(DonViDTO dv)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaDV",dv.MaDV));
               
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spDonViDelete",sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool Update(DonViDTO dv)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaDV",dv.MaDV));
                sqlparam.Add(new SqlParameter("@TenDonVi",dv.TenDonVi));
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spDonViUpdate",sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static List<DonViDTO> Select()
        {
            List<DonViDTO> lst = new List<DonViDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spDonViSelect");
                foreach (DataRow dr in dt.Rows)
                {
                    DonViDTO dv = new DonViDTO();
                    dv.MaDV = int.Parse(dr["MaDV"].ToString());
                    dv.TenDonVi = (dr["TenDonVi"].ToString());
                    lst.Add(dv);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lst;
        }
        public static DonViDTO SelectByID(int MaDV)
        {
           DonViDTO dv = new DonViDTO();
           try
           {
               List<SqlParameter> sqlparam = new List<SqlParameter>();
               sqlparam.Add(new SqlParameter("@MaDV",MaDV));

               DataTable dt = SqlDataAccessHelper.ExecuteQuery("spDonViSelectByID",sqlparam);
               DataRow dr = dt.Rows[0];
               
               dv.MaDV = int.Parse(dr["MaDV"].ToString());
               dv.TenDonVi = (dr["TenDonVi"].ToString());
               
           }
           
           catch (Exception ex)
           {
               throw ex;
           }
            return dv;
        }
    }
}

 
